package com.rpa.server.servicerpaserver.util;


import cn.hutool.core.collection.CollUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.rpa.secure.servicerpasecure.common.constant.UrlConstant;
import com.rpa.server.servicerpaserver.crawler.operate.LogonOperate;
import com.rpa.server.servicerpaserver.pojo.Cookies;
import org.openqa.selenium.Cookie;
import org.openqa.selenium.chrome.ChromeDriver;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.Set;
@Component
public class CookieSerialize {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private LogonOperate logonOperate;
    private ObjectMapper objectMapper=new ObjectMapper();




    public  void saveCookies(String key, Set<Cookie> cookies)  {
        try {
            String cookiesJson = objectMapper.writeValueAsString(cookies);
            stringRedisTemplate.opsForValue().set(key,cookiesJson);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
    }
    public  Set<Cookies> getCookies(String key)  {
        Set<Cookies> cookieSet =null;
        try {
            String cookiesJson = stringRedisTemplate.opsForValue().get(key);
            //objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
            cookieSet= objectMapper.readValue(cookiesJson, new TypeReference<Set<Cookies>>() {
            });
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }finally {
            return cookieSet;
        }
    }

    public String getCookieString(String accountId){
        Set<Cookies> cookies = this.getCookies(accountId);
        StringBuilder cookieString = new StringBuilder();
        for (Cookies cookie : cookies){
            cookieString.append(cookie.getName()).append("=").append(cookie.getValue()).append(";");
        }
        return cookieString.toString();
    }


    public boolean delCookies(String key){
       return stringRedisTemplate.delete(key);
    }

    public Set<Cookies> loadAndSetCookies(ChromeDriver chromeDriver, String idString) {
        Set<Cookies> cookies = this.getCookies(idString);
        if (CollUtil.isEmpty(cookies)) {
            logonOperate.logonCX(Long.valueOf(idString));
            cookies = this.getCookies(idString);
        }
        for (Cookie cookie : cookies) {
            chromeDriver.manage().addCookie(cookie);
        }
        return cookies;
    }

    public String getCookieValue( String accountId,String cookieName) {
        Set<Cookies> cookies = this.getCookies(accountId);
        for (Cookie cookie : cookies) {
            if (cookie.getName().equals(cookieName)) {
                return cookie.getValue();
            }
        }
        return null;
    }

    public void goFZF(String accountId,String cookieName,ChromeDriver chromeDriver){
        String cookieValue = this.getCookieValue(accountId, cookieName);
        if (cookieValue != null) {
            chromeDriver.get(UrlConstant.FZF_CHAOXING_1);
        }else{
            Set<Cookies> cookies = this.getCookies(accountId);
            boolean flagForAns = false;
            for (Cookie cookie : cookies) {
                if (cookie.getDomain().equals("mooc2-ans.chaoxing.com")) {
                   flagForAns = true;
                }
            }
            if(flagForAns){
                chromeDriver.get(UrlConstant.FZF_CHAOXING_2);
            }else {
                chromeDriver.get(UrlConstant.FZF_CHAOXING);
            }

        }
        this.loadAndSetCookies(chromeDriver, accountId);
    }
    public void goFZF1(String accountId,ChromeDriver chromeDriver){
        Set<Cookies> cookies = this.getCookies(accountId);
        boolean flagForAns = false;
        boolean flagFormooc1 = false;
        for (Cookie cookie : cookies) {
            if (cookie.getDomain().equals("mooc2-ans.chaoxing.com")) {
                flagForAns=true;
            }
            if (cookie.getDomain().equals("mobilelearn.chaoxing.com")) {
                flagFormooc1=true;
            }
        }
        if (flagForAns){
            chromeDriver.get(UrlConstant.FZF_CHAOXING_2);
        }else if (flagFormooc1){
            chromeDriver.get(UrlConstant.FZF_CHAOXING_1);
        }else{
            chromeDriver.get(UrlConstant.FZF_CHAOXING);
        }
        this.loadAndSetCookies(chromeDriver, accountId);
    }

    public void delAfterSaveCookies(String accountId, Set<Cookie> cookies){
        this.delCookies(accountId);
        this.saveCookies(accountId, cookies);
    }
}
